package cn.edu.uestc.cwdsj.service;

import cn.edu.uestc.cwdsj.domain.Jingbiao;
import cn.edu.uestc.cwdsj.utils.constant.RoleConstants;
import cn.edu.uestc.framework.auth.annotation.TKRoleRequired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class Testjinechaxun {
   @Resource
    private Testreal re;
    @TKRoleRequired(roleId = RoleConstants.ROLE_ANY)
    public List<Jinechaxun> getjinechaxunList(){
        List<Jinechaxun> jinechaList=new ArrayList<Jinechaxun>();
        List<Real> l2=new ArrayList<Real>(re.getRealList());
        Set<String> uniqueotherunitSet = new HashSet<String>();
        for(Real i:l2){
            if((i.getSubj().indexOf("1011")!=-1)&&(i.getOtherunit()!=null)) {
                uniqueotherunitSet.add(i.getOtherunit());
            }
        }
        for(String j: uniqueotherunitSet){

            Jinechaxun ji=new Jinechaxun();
            List<Jingbiao> li=new ArrayList<Jingbiao>();
            ji.setOtherunit(j);
            Long amonut=0L;
            Long bishu=0L;
            for(Real i: l2) {
                if((i.getOtherunit()!=null)&&(i.getSubj().indexOf("1011")!=-1)){
                    for(Real h:l2){
                        if((h.getUniNo()==i.getUniNo()&&(h.getOtherunit()==null))){
                            i.setChargeNam(h.getChargeNam());
                            i.setChargeSno(h.getChargeSno());
                        }
                    }
                    if(j.equals(i.getOtherunit())){
                        Jingbiao jingb=new Jingbiao();
                        jingb.setAcnt(i.getAcnt());
                        jingb.setBankno(i.getBankno());
                        jingb.setChargeNam(i.getChargeNam());
                        jingb.setChargeSno(i.getChargeSno());
                        jingb.setCheckName(i.getCheckName());
                        jingb.setDamount(i.getDamount());
                        amonut+=i.getDamount();
                        bishu++;
                        jingb.setZph(i.getZph());
                        jingb.setJamount(i.getJamount());
                        jingb.setJsfsCode(i.getJsfsCode());
                        jingb.setPrjName(i.getPrjName());
                        jingb.setInputName(i.getInputName());
                        jingb.setPrjOrder(i.getPrjOrder());
                        jingb.setSabstract(i.getSabstract());
                        jingb.setSday(i.getSday());
                        jingb.setSmonth(i.getSmonth());
                        jingb.setSubj(i.getSubj());
                        jingb.setSubjname(i.getSubjname());
                        jingb.setSyear(i.getSyear());
                        jingb.setUniNo(i.getUniNo());
                        jingb.setBeizhu(i.getBeizhu());
                        jingb.setYichangyufou(i.getYichangyufou());
                        li.add(jingb);
                    }
                }
            }
            ji.setJingbiaoList(li);
            Zongzong zz=new Zongzong();
            zz.setAmount(amonut);
            zz.setBishu(bishu);
            zz.setAmchubi(amonut,bishu);
            ji.setZozo(zz);
            jinechaList.add(ji);
        }
        Comparator<Jinechaxun> comparator = new Comparator<Jinechaxun>(){
            public int compare(Jinechaxun s1, Jinechaxun s2) {
                return -( s1.getZozo().getAmchubi()-s2.getZozo().getAmchubi()>0?1: (s1.getZozo().getAmchubi()==s2.getZozo().getAmchubi()?0:-1));
            }
        };
        Collections.sort(jinechaList,comparator);
        return jinechaList;
    }
}
